<template>
  <div class="home-container">
    <div class="header">
      <Header /> 
    </div>
    <div class="carousel-wrapper">
      <el-carousel 
        height="400px" 
        :autoplay="true" 
        indicator-position=""
        arrow="hover"
      >
        <el-carousel-item>
          <img 
            :src="swiper1" 
            alt="轮播图1" 
            class="carousel-img"
          >
        </el-carousel-item>
        <el-carousel-item>
          <img 
            :src="swiper2" 
            alt="轮播图2" 
            class="carousel-img"
          >
        </el-carousel-item>
        <el-carousel-item>
          <img 
            :src="swiper3" 
            alt="轮播图3" 
            class="carousel-img"
          >
        </el-carousel-item>
      </el-carousel>
    </div>
    
    <div class="category-section">
      <h2 class="section-title">商品分类</h2>
      <div class="category-tabs">
        <el-tabs 
          v-model="activeCategory" 
          type="card"
          @tab-change="handleCategoryChange"
          class="custom-tabs"
        >
          <el-tab-pane label="全部商品" name="0"></el-tab-pane>
          <el-tab-pane label="手机数码" name="1"></el-tab-pane>
          <el-tab-pane label="电脑办公" name="0"></el-tab-pane>
        </el-tabs>
      </div>
    </div>
    
    <div class="product-list-section">
      <el-row :gutter="20">
        <el-col 
          :span="6"
          :xs="12"
          :sm="6"
          v-for="goods in goodsList" 
          :key="goods.id"
          class="product-col"
        >
          <el-card class="product-card" hoverable>
            <template #header>
              <div class="product-img-container">
                <el-image 
                  :src="goods.goods_icon" 
                  :alt="goods.goods_desc" 
                  class="product-img"
                  @click="$router.push(`/product/${goods.id}`)"
                  fit="cover"
                ></el-image>
                <span v-if="goods.goods_stock_count === 0" class="sold-out-tag">已售罄</span>
              </div>
            </template>
            <div class="product-info">
              <h3 
                class="product-name" 
                @click="$router.push(`/product/${goods.id}`)"
                :title="goods.goods_desc"
              >
                {{ goods.goods_desc }}
              </h3>
              <div class="product-meta">
                <span class="product-price">¥{{ goods.goods_price.toFixed(2) }}</span>
                <span class="product-sales">销量: {{ goods.goods_sale_count }}</span>
              </div>
              <el-button 
                type="primary" 
                class="add-cart-btn"
                :disabled="goods.goods_stock_count === 0"
                @click="handleAddToCart(goods)"
              >
                {{ goods.goods_stock_count === 0 ? '已售罄' : '加入购物车' }}
              </el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>
      
      <!-- 空状态处理 -->
      <div v-if="goodsList.length === 0" class="empty-state">
        <el-empty description="暂无商品数据"></el-empty>
      </div>
    </div>
  </div>
</template>

<script setup name="Home">
import { ref, onMounted } from 'vue'
import { ElMessage, ElEmpty } from 'element-plus'
import axios from '../utils/axios'
import Header from '../components/Header.vue'
import useCartStore from '../store/cart'

// 引入轮播图图片
import swiper1 from '../assets/swiper1.jfif'
import swiper2 from '../assets/swiper2.jfif'
import swiper3 from '../assets/swiper3.jfif'

const cartStore = useCartStore()
const goodsList = ref([])
const activeCategory = ref('0')

const fetchGoodsByCategory = async (category) => {
  try {
    const params = {
  currentPage: 1, 
  goods_category: category,
  pageSize: category === '0' ? 4 : 8
}
    
    if (category !== '0') {
      params.goods_category = category
    }
    
    const res = await axios.get('/goods/getGoodsPage', { params })
    
    if (res.code === 200) {
      goodsList.value = res.data || []
    }
  } catch (error) {
    console.error('获取商品失败:', error)
    ElMessage.error('加载商品失败')
  }
}

// 分类切换处理
const handleCategoryChange = (category) => {
  fetchGoodsByCategory(category)
}

// 加入购物车处理
const handleAddToCart = async (goods) => {
  if (goods.goods_stock_count === 0) {
    ElMessage.warning('商品已售罄')
    return
  }
  const result = await cartStore.addToCart(goods.id)
  if (result.success) {
    ElMessage.success('加入购物车成功')
  } else {
    ElMessage.error(result.message)
  }
}

onMounted(() => {
  fetchGoodsByCategory(0)
})
</script>

<style scoped>
.home-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 1000;
}

.carousel-wrapper {
  margin: 20px 0 30px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.carousel-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 分类区域样式 */
.category-section {
  margin: 30px 0;
}

.section-title {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 16px;
  color: #333;
  position: relative;
  padding-left: 8px;
}

.section-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 20px;
  background-color: #409eff;
  border-radius: 2px;
}

.custom-tabs {
  width: 100%;
}

/* 商品列表样式 */
.product-list-section {
  padding: 20px 0 40px;
}

.product-col {
  margin-bottom: 20px;
}

.product-card {
  height: 100%;
  display: flex;
  flex-direction: column;
  transition: all 0.3s ease;
  border: 1px solid #f2f2f2;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.08);
  border-color: #e0e0e0;
}

.product-img-container {
  position: relative;
  padding-top: 100%; /* 正方形比例 */
  overflow: hidden;
  border-radius: 4px;
}

.product-img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: transform 0.3s ease;
}

.product-card:hover .product-img {
  transform: scale(1.05);
}

.sold-out-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  z-index: 1;
}

.product-info {
  padding: 12px 8px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

.product-name {
  font-size: 14px;
  line-height: 1.4;
  margin-bottom: 8px;
  color: #333;
  cursor: pointer;
  display: -webkit-box;
  overflow: hidden;
  height: 38px;
}

.product-name:hover {
  color: #409eff;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.product-price {
  color: #f56c6c;
  font-weight: 600;
  font-size: 16px;
}

.product-sales {
  font-size: 12px;
  color: #999;
}

.add-cart-btn {
  width: 100%;
  margin-top: auto;
}

.add-cart-btn:disabled {
  background-color: #f5f5f5;
  color: #999;
  border-color: #e0e0e0;
  cursor: not-allowed;
}

/* 空状态样式 */
.empty-state {
  margin: 60px 0;
  text-align: center;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .home-container {
    padding: 0 12px;
  }
  
  .carousel-wrapper {
    margin: 15px 0 20px;
  }
  
  .el-carousel {
    height: 300px !important;
  }
}

@media (max-width: 768px) {
  .el-carousel {
    height: 220px !important;
  }
  
  .section-title {
    font-size: 18px;
  }
}

@media (max-width: 576px) {
  .el-carousel {
    height: 180px !important;
  }
  
  .product-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}
</style>
    